package com.cy.dda.service.statistics.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.dao.mapper.statistics.StatisticsCollectionRecordMapper;
import com.cy.dda.dao.mapper.statistics.StatisticsRepayRecordMapper;
import com.cy.dda.model.domain.statistics.StatisticsCollectionRecord;
import com.cy.dda.model.domain.statistics.StatisticsRepayRecord;
import com.cy.dda.model.dto.statistics.MaintainStatisticsDTO;
import com.cy.dda.service.BaseService;
import com.cy.dda.service.statistics.MaintainStatisticsService;

/**
 * 维护数据统计
 *
 * @author: tanyilin
 * @create: 2018年11月5日 上午10:33:32
 */
@Service
public class MaintainStatisticsServiceImpl extends BaseService implements MaintainStatisticsService {

	@Autowired
	private StatisticsCollectionRecordMapper statisticsCollectionRecordMapper;
	@Autowired
	private StatisticsRepayRecordMapper statisticsRepayRecordMapper;
	
	@Override
	@Transactional
	public void maintainStatisticsCollectionRecord(Date date) {

		Date startTime = CommonUtils.getDayStartTime(date);
		Date endTime = CommonUtils.getDayEndTime(date);
		MaintainStatisticsDTO dto = statisticsCollectionRecordMapper.statisticsCollectionRecord(startTime, endTime);
		state(dto != null, "维护统计催收记录异常");
		
		//得到要统计的日期的年月日
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		
		//设置查询条件
		StatisticsCollectionRecord statisticsCollectionRecord = new StatisticsCollectionRecord();
		statisticsCollectionRecord.setYear(year);
		statisticsCollectionRecord.setMonth(month);
		statisticsCollectionRecord.setDay(day);
		String dateStr = year + "-" + month + "-" + day;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
        	statisticsCollectionRecord.setDate(sdf.parse(dateStr));
		} catch (ParseException e) {
			e.printStackTrace();
		}

        //先查询
        StatisticsCollectionRecord dbStatisticsCollectionRecord = statisticsCollectionRecordMapper.selectOne(statisticsCollectionRecord);
        
        Date time = new Date();
        statisticsCollectionRecord.setUpdateTime(time);
        statisticsCollectionRecord.setCount(dto.getCount());
        
        if(dbStatisticsCollectionRecord == null) {
        	//不存在，则新增一个数据
        	statisticsCollectionRecord.setCreateTime(time);
        	statisticsCollectionRecordMapper.insertSelective(statisticsCollectionRecord);
        }else {
        	//存在，则更新数据
        	statisticsCollectionRecord.setId(dbStatisticsCollectionRecord.getId());
        	statisticsCollectionRecordMapper.updateByPrimaryKeySelective(statisticsCollectionRecord);
        }
		
	}

	@Override
	@Transactional
	public void maintainStatisticsRepayRecord(Date date) {

		Date startTime = CommonUtils.getDayStartTime(date);
		Date endTime = CommonUtils.getDayEndTime(date);
		MaintainStatisticsDTO dto = statisticsRepayRecordMapper.statisticsRepayRecord(startTime, endTime);
		state(dto != null, "维护统计催收记录异常");
		
		//得到要统计的日期的年月日
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		
		//设置查询条件
		StatisticsRepayRecord statisticsRepayRecord = new StatisticsRepayRecord();
		statisticsRepayRecord.setYear(year);
		statisticsRepayRecord.setMonth(month);
		statisticsRepayRecord.setDay(day);
		String dateStr = year + "-" + month + "-" + day;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
        	statisticsRepayRecord.setDate(sdf.parse(dateStr));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        
        //先查询
        StatisticsRepayRecord dbStatisticsRepayRecord = statisticsRepayRecordMapper.selectOne(statisticsRepayRecord);
        
        Date time = new Date();
        statisticsRepayRecord.setUpdateTime(time);
        statisticsRepayRecord.setCount(dto.getCount());
        statisticsRepayRecord.setAmount(dto.getAmount());
        
        if(dbStatisticsRepayRecord == null) {
        	//不存在，则新增一条统计数据
        	statisticsRepayRecordMapper.insertSelective(statisticsRepayRecord);
        }else {
        	//存在，则更新数据
        	statisticsRepayRecord.setId(dbStatisticsRepayRecord.getId());
        	statisticsRepayRecordMapper.updateByPrimaryKeySelective(statisticsRepayRecord);
        }
		
	}

}
